package com.sukaiyi.giteeideaplugin.utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author sukaiyi
 * @date 2020/07/01
 */
public class ImageUtils {

    private ImageUtils() {

    }

    public static BufferedImage byte2Image(byte[] buf) {
        InputStream buffIn = new ByteArrayInputStream(buf);
        try {
            return ImageIO.read(buffIn);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static BufferedImage scaleTo(BufferedImage raw, int width, int height){
        BufferedImage scaled = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics graphics = scaled.getGraphics();
        graphics.drawImage(raw,0,0,width,height,null);
        return scaled;
    }

}
